﻿<?php
class ConsumeAction extends Action{
	
	
	public function index(){//显示充值页面
		$trueUrl=$_GET['tUrl'];
		$userId=$_GET['uId'];
		$this->assign("trueUrl",$trueUrl);
		if(!empty($userId))
		{
			$this->assign("userId",$userId);
			$this->display();
		}
		else{
			$this->display("Index:index");
		}
	}
	public function req(){//充值后台
		$trueUrl=$_POST['tUrl'];
		$userId=$_POST['uId'];
		$this->assign("trueUrl",$trueUrl);
   if(empty($userId))
		{
		   $this->display("Index:index");
		   return ;
		}		
	 Vendor('HttpClient');
	 $order=rand(1000,9999);
	 $order="joying_".date("YmdHij").$order;
	 $this->assign("userId",$userId);	
	 
	 $form=D("Form");	
	 $turl=base64_decode($trueUrl);
	 $dataF=$form->where("trueurl='".$turl."'")->find();
        
	#商户订单号.提交的订单号必须在自身账户交易中唯一.
	$p2_Order			=$order;	
	
	#支付卡面额
	$p3_Amt				= $_POST['p3_Amt'];
	
	#商户接收交易结果通知的地址,易宝支付主动发送支付结果(服务器点对点通讯).通知会通过HTTP协议以GET方式到该地址上.
	$p8_Url				="http://wap.szbao.net/frontend/index.php/Consume/callback";
	
	      $consume=D("Consume");
		  	$datac['fid']=$dataF['id'];		 
		  	$datac['uid']=$userId;	 	
		  	$datac['count']=$p3_Amt;
		  	$datac['consume_time']=date("Y-m-d H:i:s");
		  	$datac['flag']=0;
		  	$ddd=$consume->add($datac);
	#商家扩展信息.
	$pa_MP				=$userId."a".$ddd;	
	
	#支付卡序列号.
	$pa7_cardNo		= $_POST['pa7_cardNo'];
	
	#支付卡密码.
	$pa8_cardPwd	= $_POST['pa8_cardPwd'];

	#银行编码
	$pd_FrpId			= "SZX";	      
	annulCard($p2_Order,$p3_Amt,$p8_Url,$pa_MP,$pa7_cardNo,$pa8_cardPwd,$pd_FrpId);	    
	$this->display("consume2");
	return ;		
	}
	public function callback(){echo "success";	//回调后台
		#	解析返回参数.	
	$return = getCallBackValue($r0_Cmd,$r1_Code,$rb_Order,$r2_TrxId,$rq_CardNo,$rq_ReturnMsg,$pa_MP,$rc_Amt,$hmac);
	#	判断返回签名是否正确（True/False）
	$bRet = CheckHmac($r0_Cmd,$r1_Code,$rb_Order,$r2_TrxId,$rq_CardNo,$rq_ReturnMsg,$pa_MP,$rc_Amt,$hmac);
	#	以上代码和变量不需要修改.	 	      
	#	校验码正确.
	
	$dat=split("a",$pa_MP);	 	
	if($bRet){		  
		  if($r1_Code=="1"){
		  	$consume=D("Consume");
		  	$datac['id']=$dat[1];		  	  	
		  	$datac['flag']=1;
		  	$datac['consume_time']=date("Y-m-d H:i:s");
		  	$consume->save($datac);		  	
		     $mUser=D("Mobileuser");  	       
		     $mUser->where("uid=".$dat[0])->find();		   
		     $data=array();
		     $data['uid']=$dat[0];
		     $score=0;
		     switch($rc_Amt){
		     	case 30:
		     	  $score=35;
		     	  break;
		     	case 50: $score=60;
		     					break;
		     	case 100:$score=140;
		     						break;
		     	case 300:$score=440;
		     						break;
		     	default:
		    					 	break;
		     }
		     $data['score']=$mUser->score+$score;	     
		     $data['last_consume_time']=date("Y-m-d H:i:s");	     
		     $mUser->save($data);
		     exit; 
		  } else if($r1_Code=="2"){
		  	$consume=D("Consume");
		  	$datac['id']=$dat[1];	 	  	
		  	$datac['flag']=-1;
		  	$datac['consume_time']=date("Y-m-d H:i:s");
		  	$consume->save($datac);			  	
		    exit; 
		} 
		}else{		
		  	 
		  	 /**
		  	 $mUser=D("Mobileuser");  	       
		     $mUser->where("uid=".$pa_MP)->find();
		     $data=array();
		     $data['uid']=$pa_MP;
		  		$data['score']=102;		      
		     $data['last_consume_time']=date("Y-m-d H:i:s");	     
		     $mUser->save($data);	
		     */
		     
		    $consume=D("Consume");
		  	$datac['id']=$dat[1];
		  	$datac['flag']=-1;
		  	$datac['consume_time']=date("Y-m-d H:i:s");
		  	$consume->save($datac);			     
		     exit;		
   	
	}
	}
	public function bef_consume(){//显示付费页面
		$trueUrl=$_GET['tUrl'];
		$userId=$_GET['uId'];
		$this->assign("trueUrl",$trueUrl);
		if(empty($userId))
		{
		  $this->display("Index:index");
		  return;
		}		
		$User=D("Form");		
		
	  if(	empty($trueUrl)){
	  
	  	header("location:http://www.baidu.com");
	  	return ;
	  }	
	  $data=$User->where("trueUrl='".$trueUrl."'")->find(); 
	  if($data['trueurl']==""){
	  	header("location:http://www.google.com");
	  	return;
	  }
	  
	  $this->assign("userId",$userId);
  	$mUser=D("Mobileuser");		
  	$datam=$mUser->where('userId='.$userId)->field('score')->find(); 
  	$this->assign("score",$data['score']);
  	$this->assign("mScore".$datam['score']);
  	$this->display("consume");
  	return ;
  }
		
	public function consume(){//扣费并跳转到要查看的页面
	
		$trueUrl=$_GET['tUrl'];
		$userId=$_GET['uId'];
		$this->assign("trueUrl",$trueUrl);
		if(empty($userId))
		{
		  $this->display("Index:index");
		  return;
		}						
		$this->assign("userId",$userId);
		$mUser=D("Mobileuser");
		$data=array();
		try{
				$mUser->startTrans();				
				$form=D("Form");	
				$turl=base64_decode($trueUrl);
	    	$dataF=$form->where("trueurl='".$turl."'")->find();
	    	//echo $form->getLastSQL();
	    	//var_dump($dataF);
	    	if(empty($dataF)){
	    		header("location:http://www.google.com");
	    		return ;
	    	}
	    	$data=$mUser->where('uid='.$userId)->find(); 
	    	
	    	$data['score']=$data['score']-$dataF['score'];		   
		if($data['score']<0){
			$message="您的点数不足，请先充值";
			$this->assign("msg_consume",$message);
			$this->display("index");
			return;
		}
		$data['last_consume_time']=date("Y-m-d H;i:s");
		$mUser->save($data);		
		$count=D("Count");
		$count->fakeUrlId=$dataF['id'];		
		$count->muid=$data['uid'];
		$count->add();
		$mUser->commit() ;
    }catch(Exception $e){
   	$mUser->rollback() ;
   	$message="系统出错，请稍候再试";    
   	$this->assign("msg",$message);
   	$this->assign("urlName","返回首页");
  	$this->assign("url","Index:index");
  	$this->display("Public:success");	
  	return;   	
    }		
		header("location:http://$turl");
		return;		
	}
}


?>